import { reqGoodInfo ,reqAddOrUpdateShopCart } from '@/api/index'
import { getUUid } from '@/utils/uuid_token';
const state = {
    goodInfo: {},//商品信息
    shopCartCode: 201,//购物车信息
    uuid_token:getUUid(),
};
const actions = {
    async getGoodInfo({ commit ,state}, skuId) {
        let res = await reqGoodInfo(skuId);
        if (res.code == 200) {
            commit('GETGOODINFO',res.data);
        }
        else console.log("请求失败");
    },
    async AddOrUpdateShopCart({ commit }, { skuId, skuNum }) {
        let res = await reqAddOrUpdateShopCart(skuId, skuNum);
        state.shopCartCode = res.code;//存储状态码判断家加入购物车是否成功
        if (res.code == 200) {
            console.log('ok');
            return 'ok';
        }
        else {
            return Promise.reject(new Error('faile'));
        }
    }
};
const mutations = {
    GETGOODINFO(state, data) {
        state.goodInfo = data;
    },
    ADDORUPDATESHOPCART(state, data) {
        state.shopCart = data;
    }
};

//什么时候需要用到getters，当服务器一次返回的数据比较复杂时(多个数据一次集成发送时，可以简化数据)
const getters = {//计算属性都是简化数据的
    categoryView(state) {
        return state.goodInfo.categoryView || {};//如果没有数据返回空对象，后期访问报错
    },
    skuInfo(state) {
        return state.goodInfo.skuInfo || {};//防止underfined
    },
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList||[];
    }
};

export default {
    state,
    actions,
    mutations,
    getters
}